const { ethers, network } = require("hardhat")
const { resolve } = require("path")
const { devlopmentChains } = require("../helper-hardhat-config")

module.exports = async (hre) => {
    const { getNamedAccounts, deployments } = hre

    const { deployer } = await getNamedAccounts() //获取部署合约的地址

    //BasicNFt mint
    const BasicNFT = await ethers.getContract("BasicNFT", deployer)
    await (await BasicNFT.mintNft()).wait(1)
    console.log(`BasicNFT tokenURI ${(await BasicNFT.tokenURI(1)).toString()}`)

    //RandomIpfsNFT
    const RandomIpfsNFT = await ethers.getContract("RandomIpfsNFT", deployer)
    const mintFee = await RandomIpfsNFT.getMintFee()

    await new Promise(async (resolve, reject) => {
        setTimeout(() => reject("minted fail timeOut"), 60000)
        RandomIpfsNFT.once("NftMinted", async () => {
            // @ts-ignore
            resolve()
        })
        const mintRecript = await (
            await RandomIpfsNFT.requestNFT({ value: mintFee })
        ).wait(1)

        if (devlopmentChains.includes(network.name)) {
            const VRFCoordinatorV2Mock = await ethers.getContract(
                "VRFCoordinatorV2Mock",
                deployer
            )
            await VRFCoordinatorV2Mock.fulfillRandomWords(
                mintRecript.events[1].args.requestId.toString(),
                RandomIpfsNFT.address
            )
        }
    })
    console.log(
        `RandomIpfsNFT tokenURI ${(await RandomIpfsNFT.tokenURI(1)).toString()}`
    )

    //DynamicNFT
    const DynamicNFT = await ethers.getContract("DynamicSvgNFT", deployer)
    const Hval = ethers.utils.parseEther("2100")
    const Lval = ethers.utils.parseEther("1")

    await (await DynamicNFT.mintNFT(Hval)).wait(1)
    console.log(
        `DynamicNFT HigtokenURI ${(await DynamicNFT.tokenURI(1)).toString()}`
    )

    await (await DynamicNFT.mintNFT(Lval)).wait(1)
    console.log(
        `DynamicNFT LowtokenURI ${(await DynamicNFT.tokenURI(2)).toString()}`
    )
}

module.exports.tags = ["mint"]
